#!/bin/bash
function checkChange {
  echo "check $1"
  s=`cat ../log |grep "$1/"|wc -l`
  if [[ s -gt 0 ]]; then
    if [[ $2 -gt 0 ]]; then
      build $1 $2
    else
      install $1
    fi
  fi
}
function install {
  app=$1
  echo "install $app"
  cd $app
  mvn clean install -DskipTests && \
  mvn clean
  cd ../
}
function build {
  app=$1
  port=$2
  echo "build $app"
  cd $app
  mvn clean package -DskipTests && \
  docker ps -f name=$app -q -a|xargs -t -I{} docker rm -f -v {} && \
  docker images chrix/$app -q|xargs -t -I{} docker rmi -f {} && \
  docker build -t chrix/$app:latest . && \
  docker container create --name="$app" --add-host my.com:192.168.2.22 --publish-all -p $port:$port chrix/$app:latest && \
  docker container start $app && \
  docker image prune
  mvn clean
  cd ../
}

if [ ! -e .git ]; then
  git init && \
  git clone git@gitee.com:chris_k/coupon-chrix.git
  echo ' pom.xml chrix-common/ chrix-config/ chrix-eureka/ chrix-oauth2/ chrix-user/ chrix-gateway/ chrix-app/ chrix-admin/ code-generator/' > log
else
  git pull git@gitee.com:chris_k/coupon-chrix.git master > log
fi
cat log

cd coupon-chrix
s=`cat ../log|grep ' pom.xml '|wc -l`
if [[ s -gt 0 ]]; then
  echo "build parent project"
  mvn clean install -DskipTests
fi

checkChange chrix-common
checkChange chrix-config 6002
checkChange chrix-eureka 6001
checkChange chrix-oauth2 6006
checkChange chrix-user 6007
checkChange chrix-gateway 6004
checkChange chrix-app 6008
checkChange chrix-admin 6003
#checkChange code-generator